package com.service.supply;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.model.purchase.SupplyCompeteRecordModel;
import com.model.supply.SupplyUserModel;
import com.model.sys.SysUser;
import com.common.spring.SpringMvcUtils;
import com.dao.purchase.SupplyCompeteRecordDao;

@Service("SupplyCompeteRecordService")
public class SupplyCompeteRecordService {
	static final Logger logger = LoggerFactory.getLogger(SupplyCompeteRecordService.class);

	@Autowired
	SupplyCompeteRecordDao supplyCompeteRecordDao;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		SupplyUserModel supplyUserModel = SpringMvcUtils.getSupplyUser();
		if (supplyUserModel == null) {
			return HttpResult.error("用户不存在！");
		}
		queryMap.put("infId", supplyUserModel.getInfId());

		List<SupplyCompeteRecordModel> list = supplyCompeteRecordDao.selectList(queryMap);
		int count = supplyCompeteRecordDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String infId, String inqId, String comUuid) {
		HttpResult httpResult = new HttpResult();
		SupplyCompeteRecordModel supplyCompeteRecordModel = supplyCompeteRecordDao.load(infId, inqId, comUuid);
		httpResult.setData(supplyCompeteRecordModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	public HttpResult save(SupplyCompeteRecordModel supplyCompeteRecordModel) {
		if (supplyCompeteRecordModel == null) {
			return HttpResult.error("参数不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		if (!StringUtils.isNotNull(supplyCompeteRecordModel.getInfId())) {
			supplyCompeteRecordModel.setInfId(UUID.randomUUID().toString());
			result = supplyCompeteRecordDao.insert(supplyCompeteRecordModel);
		} else {
			result = supplyCompeteRecordDao.update(supplyCompeteRecordModel);
		}
		if (result > 0) {
			httpResult.setMsg(supplyCompeteRecordModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(supplyCompeteRecordModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(supplyCompeteRecordModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 提交
	 *
	 * @param
	 * @return
	 */
	public HttpResult commit(SupplyCompeteRecordModel supplyCompeteRecordModel) {
		if (supplyCompeteRecordModel == null) {
			return HttpResult.error("参数不能为空");
		}
		supplyCompeteRecordModel.setStatus(Global.YES + "");
		HttpResult httpResult = new HttpResult();
		int result = 0;
		result = supplyCompeteRecordDao.update(supplyCompeteRecordModel);
		if (result > 0) {
			httpResult.setMsg(supplyCompeteRecordModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
			httpResult.setData(supplyCompeteRecordModel);
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg(supplyCompeteRecordModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String infId, String inqId, String comUuid) {
		if (!StringUtils.isNotNull(infId) || !StringUtils.isNotNull(inqId) || !StringUtils.isNotNull(comUuid)) {
			return HttpResult.error("参数不能为空");
		}
		SupplyCompeteRecordModel model = supplyCompeteRecordDao.load(infId, inqId, comUuid);
		if (!model.getStatus().equals(Global.NO + "")) {
			return HttpResult.error("非待提交状态，不可删除！");
		}
		HttpResult httpResult = new HttpResult();
		int result = supplyCompeteRecordDao.delete(infId, inqId, comUuid);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 撤回
	 *
	 * @param
	 * @return
	 */
	public HttpResult back(String infId, String inqId, String comUuid) {
		if (!StringUtils.isNotNull(infId) || !StringUtils.isNotNull(inqId) || !StringUtils.isNotNull(comUuid)) {
			return HttpResult.error("参数不能为空");
		}
		SupplyCompeteRecordModel model = supplyCompeteRecordDao.load(infId, inqId, comUuid);

		// todo

		HttpResult httpResult = new HttpResult();
		model.setStatus(Global.NO + "");
		int result = supplyCompeteRecordDao.update(model);
		if (result > 0) {
			httpResult.setMsg("撤回成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		List<SupplyCompeteRecordModel> list = supplyCompeteRecordDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), SupplyCompeteRecordModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
